<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <el-card shadow="never" class="login-card">
        <img class="login-logo" src="http://likede2-admin.itheima.net/img/logo.595745bd.png" alt="">
        <!--登录表单-->
        <el-form ref="loginForm" :model="loginForm" status-icon :rules="rules">
          <div class="username">
            <el-form-item prop="username">
              <i class="el-icon-mobile-phone" />
              <el-input v-model="loginForm.username" type="username" autocomplete="off" />
            </el-form-item>
          </div>
          <div class="username">
            <el-form-item prop="password">
              <i class="el-icon-lock" />
              <el-input v-model="loginForm.password" show-password type="password" autocomplete="off" />
            </el-form-item>
          </div>
          <div class="username">
            <el-form-item prop="code">
              <i class="el-icon-circle-check" />
              <el-input v-model="loginForm.code" placeholder="请输入验证码" autocomplete="off" />
            </el-form-item>
            <div class="codeimg"> <img :src="`https://likede2-java.itheima.net/api/user-service/user/imageCode/${codeImg}`" alt="" @click="getNewCode"></div>
          </div>
          <el-form-item>
            <el-button type="primary" style="width:388px" @click="submitForm">登录</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>
<script>
import nanoid from '@/utils/getRandom'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: 'admin',
        password: 'admin',
        code: ''
      },
      codeImg: '',
      clientToken: '',
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, max: 16, message: '密码在5-16位之间', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 4, max: 4, message: '请输入4位数验证码', trigger: 'blur' }
        ]
      }
    }
  },
  // 先进行获取随机码35位
  async created() {
    this.codeImg = nanoid()
  },
  methods: {
    async submitForm() {
      await this.$refs.loginForm.validate()
      // console.log('去登录')
      await this.$store.dispatch('user/login', {
        loginName: this.loginForm.username,
        password: this.loginForm.password,
        code: this.loginForm.code,
        clientToken: this.codeImg,
        loginType: 0
      })
      this.$router.push('/')
    },
    getNewCode() {
      this.codeImg = nanoid()
    }
    // async getImgCode() {
    //   this.getNanoid()
    //   const res = await getImgCode(this.clientToken)
    //   // console.log(res)
    //   const url = window.URL.createObjectURL(res.data)
    //   // console.log(url)
    //   this.codeImg = url
    // }

  }
}
</script>
<style lang="scss" scoped>
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 1;
    background: rgba(38, 72, 176) url(http://likede2-admin.itheima.net/img/background.be4fae7d.png)
      no-repeat  center -1px / cover;
  }
  .form {
    .login-logo{
      position: absolute;
      width: 96px;
      height: 96px;
      top: -46px;
      left: 50%;
      margin-left: -48px;
    }
    .el-card {
      border: none;
      padding: 0;
      position: absolute;
      width: 518px;
      height: 388px;
      top: 50%;
      left: 50%;
      margin-top: -194px;
      margin-left: -259px;
      padding: 76px 35px 0;
      background: #fff;
      box-shadow: 0 3px 70px 0 rgba(30,111,72,.35);
      border-radius: 10px;
      overflow: visible;
      .el-form{
        margin-top: -20px;
        padding-left: 20px;
      }
    }
    .username{
      width: 100%;
      height: 52px;
      margin-bottom: 24px;
      background: #fff;
      border: 1px solid #e2e2e2;
      border-radius: 4px;
      padding-left: 10px;
      position: relative;
    }
    .codeimg{
      position: absolute;
      top: 0.5px;
      right: 14px;
      width: 30%;
      height: 44px;
      background-color: #ccc;
    }
    .el-input {
      width: 350px;
      height: 44px;
      line-height: 52px;
      .el-input__inner {
        background: #fff;
        border: 1px solid transparent;
      }
    }
    .el-button{
      height: 50px;
      border-radius: 8px;
      background: linear-gradient(262deg,#2e50e1,#6878f0);
    }
  }
}
</style>
